﻿using System.Collections.Generic;
using IntegratedMeasuring.ViewModels.Entity;
using IntegratedMeasuring.ViewModels.Scheme;

namespace IntegratedMeasuring.Utils.InstrumentParser;

public class Channel48Parser : BaseParser
{
    public static void ParseAutoTransFileContent(byte[] data, out TestConditions testConditions)
    {
        testConditions = new TestConditions();
        // 文件名称
        testConditions.Name = PraseTransName(data.GetBytesByLength(1, 16));
        testConditions.SourceData = data;
        // 所有脚位 Pin_Label
        List<string> pinLabels = PrasePinLabel(data.GetBytesByLength(33657, 196)); 
        List<PinData> pins1 = new List<PinData>();
        
        byte[] temp = data.GetBytes(52, 252);
        for (int i = 0; i < temp.Length; i++)
        {
            if (temp[i] != 0)
            {
                // string pin = $"{pinLabels[temp[i] - 1]} - {pinLabels[temp[i + 1] - 1]}";
                string pin = $"{temp[i].ToInt().ToString().PadLeft(2, '0')} - {temp[i + 1].ToInt().ToString().PadLeft(2, '0')}";
                pins1.Add(new PinData
                {
                    PRI = i / 20,
                    SEC = (i / 2) % 10,
                    Pin = pin,
                });
            }
            i++;
        }
        
        List<TransItemData> transItemDatas = new List<TransItemData>();
        var SeriesPins = GenerateSeriesPins(data.GetBytes(252, 1452), 1200);
        var ParallelPins = GenerateParallelPin(data.GetBytes(1452, 1952), data.GetBytes(1952, 2452), 500);
        
        int places = 3;
        
        // Turns 
        if (data[2452] == 1)  // Turns.开关
        {
            int limitType = data[2454];
            string freq = data.GetBytes(2456, 2460).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            string voltage = data.GetBytes(2460, 2464).ToFloat().KeepPlaces2(places);
            byte[] nominals = data.GetBytes(2464, 2864);
            byte[] mins = data.GetBytes(3264, 3664);
            byte[] maxs = data.GetBytes(3664, 4064);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();
                    min = min_float == 0 ? string.Empty : $"{min_float.KeepPlaces2(places)}(T)";
                    max = max_float == 0 ? string.Empty : $"{max_float.KeepPlaces2(places)}(T)";
                }

                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        string nominal = $"{nominal_float.KeepPlaces2(3)}(T)";
                        if (SEC == 0)
                        {
                            transItemDatas.Add(
                                new TransItemData()
                                {
                                    Pri = PRI,
                                    Sec = SEC,
                                    Pins = pin,
                                    ParallelPin = ParallelPins[10 * PRI + SEC],
                                    SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 2)?.ShortPin,
                                    MinorTerm = "TURN",
                                    Freq = freq,
                                    Vol = voltage,
                                    Nominal = nominal,
                                    LowerLimit = string.Empty,
                                    UpperLimit = string.Empty,
                                }
                           );
                        }
                        else
                        {
                            transItemDatas.Add(
                                new TransItemData()
                                {
                                    Pri = PRI,
                                    Sec = SEC,
                                    Pins = pin,
                                    ParallelPin = ParallelPins[10 * PRI + SEC],
                                    SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 2)?.ShortPin,
                                    MinorTerm = "TURN",
                                    Freq = freq,
                                    Vol = voltage,
                                    Nominal = nominal,
                                    LowerLimit = min,
                                    UpperLimit = max,
                                }
                           );
                        }
                    }
                }
                i++; i++; i++;
            }
        }
        
        // Lx
        if (data[4064] == 1)
        {
            bool isLevelCurrent = false;
            isLevelCurrent = data[32641] == 1 ? true : false;  // LxsLevIsCur
            bool isMultiFreq = false;
            isMultiFreq = data[4067] == 1 ? true : false;    // lxs.多频开关
            bool isMultiVoltage = false;
            isMultiVoltage = data[9929] == 1 ? true : false;
            var multiFreqs = data.GetBytes(4078, 4478);  // lxs.多频
            int limitType = data[4068];   // lxs.极限模式
            string voltage = string.Empty;
            if (!isLevelCurrent)
            {
                voltage = data.GetBytes(4070, 4074).ToFloat().KeepPlaces2(places);  // lxs.电压
            }
            else
            {
                voltage = data.GetBytes(4070, 4074).ToFloat().UnitConversion(1000.0).KeepPlaces2(places);
            }

            string freq = data.GetBytes(4074, 4078).ToFloat().UnitConversion(0.001).KeepPlaces2(places);  // lxs.频率
            byte[] nominals = data.GetBytes(4878, 5278);  // lxs.标称
            byte[] mins = data.GetBytes(5678, 6078);    //lxs.下限
            byte[] maxs = data.GetBytes(6078, 6478);   // lxs.上限
            for (int i = 0; i < nominals.Length - 4; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();

                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;

                if (isMultiFreq)
                    freq = multiFreqs.GetBytes(i, i + 4).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();


                    if (min_float != 0)
                    {
                        string[] formatVal = CommonUtil.FormatInductance(min_float, "H");
                        min = formatVal[0] + formatVal[1];
                        // min_float.HenryUnitConversion(GC.HenryUnit.H, out min);
                    }

                    if (max_float != 0)
                    {
                        string[] formatVal = CommonUtil.FormatInductance(max_float, "H");
                        max = formatVal[0] + formatVal[1];
                        // max_float.HenryUnitConversion(GC.HenryUnit.H, out max);
                    }
                    min = min_float == 0 ? string.Empty : min;
                    max = max_float == 0 ? string.Empty : max;
                }
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        nominal_float.HenryUnitConversion(GC.HenryUnit.H, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 1)?.ShortPin,
                                MinorTerm = "Lx",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }
        
        // Q   Qs
        if (data[1665] == 1)  // Qs.开关
        {
            byte[] nominals = data.GetBytes(6478, 6878);
            byte[] mins = data.GetBytes(6878, 7278);
            byte[] maxs = data.GetBytes(32196, 32596);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
        
                string min = string.Empty;
                string max = string.Empty;
        
                // if (isMultiFreq)
                //     freq = multiFreqs.GetBytes(i, i + 4).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
                min = mins.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                max = maxs.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
        
                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        var lx = transItemDatas.Find(s => s.Pri == PRI && s.Sec == SEC && s.MinorTerm == "Lx");
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 1)?.ShortPin,
                                MinorTerm = "Q",
                                Freq = lx.Freq,
                                Vol = lx.Vol,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }

        // Lk
        if (data[13708] == 1)  // LKs.开关
        {
            bool isLevelCurrent = false;
            isLevelCurrent = data[32639] == 1 ? true : false;  // LksLevIsCur

            int limitType = data[13710];   // LKs.开关
            var multiFreqs = data.GetBytes(32115, 32155);
            var multiVoltages = data.GetBytes(32155, 32195);
            var LkShortPins = data.GetBytes(13820, 18620); // LKs.shortpin
            string freq = data.GetBytes(13716, 13720).ToFloat().UnitConversion(0.001).KeepPlaces2(places); // LKs.频率
            string voltage = string.Empty;
            if (!isLevelCurrent)
            {
                voltage = data.GetBytes(13712, 13716).ToFloat().KeepPlaces2(places);  // LKs.电压
            }
            else
            {
                voltage = data.GetBytes(13712, 13716).ToFloat().UnitConversion(1000.0).KeepPlaces2(places); // LKs.电压
            }

            byte[] testPins = data.GetBytes(13720, 13820);  
            var LkTestPins = GenerateLkTestPins(testPins);

            byte[] nominals = data.GetBytes(18620, 19020);  // LKs.标称
            byte[] mins = data.GetBytes(19420, 19820); // LKs.下限
            byte[] maxs = data.GetBytes(19820, 20220); // LKs.上限
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;
                float freqTemp = multiFreqs.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (freqTemp > 0)
                {
                    freq = freqTemp.UnitConversion(0.001).KeepPlaces2(places);
                }
                float volTemp = multiVoltages.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (volTemp > 0)
                {
                    voltage = volTemp.KeepPlaces2(places);
                }
                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();
                    if (min_float != 0) min_float.HenryUnitConversion(GC.HenryUnit.H, out min);
                    if (max_float != 0) max_float.HenryUnitConversion(GC.HenryUnit.H, out max);
                    min = min_float == 0 ? string.Empty : min;
                    max = max_float == 0 ? string.Empty : max;
                }
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        var shortPins = LkShortPins.GetBytes(480 * PRI + 48 * SEC, 480 * PRI + 48 * (SEC + 1));
                        string shortPin = string.Empty;
                        shortPin = GenerateLkPin(shortPins);

                        var LkPin = LkTestPins.Find(s => s.PRI == PRI && s.SEC == SEC);
                        int windingIndex = LkPin.Pin;
                        string testPin = pins1.Find(e => e.PRI == PRI && e.SEC == windingIndex).Pin;
                        nominal_float.HenryUnitConversion(GC.HenryUnit.H, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = testPin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 0)?.ShortPin,
                                ShortCircuitPin = shortPin,
                                MinorTerm = "Lk",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }

        // DCR
        if (data[11698] == 1)  // Dcrs.开关
        {
            int limitType = data[11699];  // Dcrs.极限模式
            string freq = string.Empty;
            string voltage = string.Empty;
            byte[] nominals = data.GetBytes(12108, 12508);  // Dcrs.标称
            byte[] mins = data.GetBytes(12908, 13308);  // Dcrs.下限
            byte[] maxs = data.GetBytes(13308, 13708);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string min = string.Empty;
                string max = string.Empty;

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min_float = mins.GetBytes(i, i + 4).ToFloat();
                    max_float = maxs.GetBytes(i, i + 4).ToFloat();
                    if (min_float != 0) min_float.OhmUnitConversion(GC.OhmUnit.Ω, out min);
                    if (max_float != 0) max_float.OhmUnitConversion(GC.OhmUnit.Ω, out max);
                    min = min_float == 0 ? string.Empty : min;
                    max = max_float == 0 ? string.Empty : max;
                }
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        nominal_float.OhmUnitConversion(GC.OhmUnit.Ω, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 5)?.ShortPin,
                                MinorTerm = "DCR",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }

        // ACR
        if (data[9688] == 1)
        {
            int limitType = data[9689];
            string freq = data.GetBytes(9690, 9694).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            string voltage = data.GetBytes(9694, 9698).ToFloat().KeepPlaces2(places);
            byte[] nominals = data.GetBytes(10098, 10498);
            byte[] mins = data.GetBytes(10898, 11298);
            byte[] maxs = data.GetBytes(11298, 11698);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                string min = 0.ToString($"F{3}");
                string max = 0.ToString($"F{3}");
                

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min = mins.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                    max = maxs.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);
                }
                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 3)?.ShortPin,
                                MinorTerm = "ACR",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                        );
                    }
                }
                i++; i++; i++;
            }
        }

        // Cx
        if (data[20220] == 1)
        {
            int limitType = data[20222];
            var CxPins1 = data.GetBytes(20232, 22032);
            var CxPins2 = data.GetBytes(22032, 23832);
            var ShortPins =  data.GetBytes(35863, 36463);
            var multiFreqs = data.GetBytes(26512, 26552);
            var multiVoltages = data.GetBytes(26912, 26952);

            // updated by @suns 2022-04-18
            string voltage = data.GetBytes(20224, 20228).ToFloat().KeepPlaces2(places);
            string freq = data.GetBytes(20228, 20232).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            byte[] nominals = data.GetBytes(23832, 24232);
            byte[] mins = data.GetBytes(24632, 25032);
            byte[] maxs = data.GetBytes(25032, 25432);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().UnitConversion(1000000000000000.0).KeepPlaces2(places);
                string min = 0.ToString($"F{3}");
                string max = 0.ToString($"F{3}");

                float freqTemp = multiFreqs.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (freqTemp > 0)
                {
                    freq = freqTemp.UnitConversion(0.001).KeepPlaces2(places);
                }
                float volTemp = multiVoltages.GetBytes(4 * SEC, 4 * SEC + 4).ToFloat();
                if (volTemp > 0)
                {
                    voltage = volTemp.KeepPlaces2(places);
                }

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min = mins.GetBytes(i, i + 4).ToFloat().UnitConversion(1000000000000000.0).KeepPlaces2(places);
                    max = maxs.GetBytes(i, i + 4).ToFloat().UnitConversion(1000000000000000.0).KeepPlaces2(places);
                    min = (double.Parse(min) / 1000).ToString("f2") + "(pF)";
                    max = (double.Parse(max) / 1000).ToString("f2") + "(pF)";
                }
                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        var testPins = CxPins1.GetBytes(180 * PRI + 18 * SEC, 180 * PRI + 18 * (SEC + 1))
                            .Append(CxPins2.GetBytes(180 * PRI + 18 * SEC, 180 * PRI + 18 * (SEC + 1)));
                        string testPin = string.Empty;
                        testPin = GenerateCxPin(testPins);
                        var shortPins = ShortPins.GetBytes(60 * PRI + 6 * SEC, 60 * PRI + 6 * (SEC + 1));
                        string shortPin = GenerateCxShortPin(shortPins);
             
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = testPin,
                                // ParallelPin = ParallelPins[10 * PRI + SEC],
                                ShortCircuitPin = shortPin,
                                MinorTerm = "Cx",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                        );
                    }
                }
                i++; i++; i++;
            }
        }
        
        // PS
        if (data[25432] == 1)  // Pss.开关
        {
            temp = data.GetBytes(25433, 25633);  // Pss.脚位
            for (int i = 0; i < temp.Length; i++)
            {
                int PRI = i / 20;
                int SEC = (i / 2) % 10;
                string pin = $"Short {temp[i].ToInt().ToString().PadLeft(3, '0')} - {temp[i + 1].ToInt().ToString().PadLeft(3, '0')}";
                if (temp[i] != 0)
                {
                    transItemDatas.Add(
                        new TransItemData()
                        {
                            Pri = PRI,
                            Sec = SEC,
                            Pins = pin,
                            MinorTerm = "PS",
                        }
                    );
                }
                i++;
            }
        }
        
        // Bls  BAL
        if (data[9134] == 1)  // Bls.开关
        {
            // int limitType = data[4739];  // Dcrs.极限模式
            string freq = string.Empty;
            string voltage = string.Empty;
            byte[] nominals = data.GetBytes(25836, 26036);  // Bls.标称
            byte[] maxs = data.GetBytes(26036, 26236);  // Bls.上限
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 20;
                int SEC = (i / 4) % 5;
                float nominal_float = nominals.GetBytes(i, i + 4).ToFloat();
                float min_float = 0f;
                float max_float = 0f;
                string max = string.Empty;
           
                max_float = maxs.GetBytes(i, i + 4).ToFloat();
                if (max_float != 0) max_float.HenryUnitConversion(GC.HenryUnit.H, out max);
                max = max_float == 0 ? string.Empty : max;
                
                if (nominal_float != 0)
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        nominal_float.HenryUnitConversion(GC.HenryUnit.H, out string nominal);
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 5)?.ShortPin,
                                MinorTerm = "BAL",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                // LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }
        
        // Zs
        if (data[33854] == 1)
        {
            int limitType = data[7679];
            string voltage = data.GetBytes(7684, 7688).ToFloat().KeepPlaces2(places);
            string freq = data.GetBytes(7680, 7684).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
            bool isMultiFreq = false;
            isMultiFreq = data[33854] == 1 ? true : false;
            bool isMultiVoltage = false;
            isMultiVoltage = data[34255] == 1 ? true : false;
            var multiFreqs = data.GetBytes(33855, 34255);
            var multiVoltages = data.GetBytes(34257, 34657);

            byte[] nominals = data.GetBytes(8088, 8488);
            byte[] mins = data.GetBytes(8888, 9288);
            byte[] maxs = data.GetBytes(9288, 9688);
            for (int i = 0; i < nominals.Length; i++)
            {
                int PRI = i / 40;
                int SEC = (i / 4) % 10;
                string nominal = nominals.GetBytes(i, i + 4).ToFloat().UnitConversion(1.0).KeepPlaces2(places);
                string min = 0.ToString($"F{3}");
                string max = 0.ToString($"F{3}");
                if (isMultiFreq) freq = multiFreqs.GetBytes(i, i + 4).ToFloat().UnitConversion(0.001).KeepPlaces2(places);
                if (isMultiVoltage) voltage = multiVoltages.GetBytes(i, i + 4).ToFloat().KeepPlaces2(places);

                if (limitType == 0)
                {
                    min = $"{mins.GetBytes(i, i + 4).ToFloat()}%";
                    max = $"{maxs.GetBytes(i, i + 4).ToFloat()}%";
                }
                else
                {
                    min = mins.GetBytes(i, i + 4).ToFloat().UnitConversion(1.0).KeepPlaces2(places);
                    max = maxs.GetBytes(i, i + 4).ToFloat().UnitConversion(1.0).KeepPlaces2(places);
                }

                if (nominal != 0.ToString($"F{3}"))
                {
                    if (pins1.Exists(e => e.PRI == PRI && e.SEC == SEC))
                    {
                        string pin = pins1.Find(e => e.PRI == PRI && e.SEC == SEC).Pin;
                        min = min == 0.ToString($"F{3}") ? string.Empty : min;
                        max = max == 0.ToString($"F{3}") ? string.Empty : max;
                        transItemDatas.Add(
                            new TransItemData()
                            {
                                Pri = PRI,
                                Sec = SEC,
                                Pins = pin,
                                ParallelPin = ParallelPins[10 * PRI + SEC],
                                SeriesPin = SeriesPins.Find(s => s.PRI == PRI && s.SEC == SEC && s.ItemIndex == 4)?.ShortPin,
                                MinorTerm = "Zx",
                                Freq = freq,
                                Vol = voltage,
                                Nominal = nominal,
                                LowerLimit = min,
                                UpperLimit = max,
                            }
                       );
                    }
                }
                i++; i++; i++;
            }
        }
        
        testConditions.BeautifyData = transItemDatas;
    }
}